package com.woniuxy.oa.rms.login.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.oa.rms.login.entity.SessionInfo;
import com.woniuxy.oa.rms.login.entity.User;
import com.woniuxy.oa.rms.login.listener.RightListener;
import com.woniuxy.oa.rms.login.service.imp.BaseServiceImp;
import com.woniuxy.oa.rms.login.service.imp.SessionInfoServiceImp;
import com.woniuxy.oa.rms.login.service.imp.UserServiceImp;

@Controller
@SuppressWarnings("all")
@RequestMapping("/loginController")
public class indexController {
	@Resource
	private UserServiceImp loginService;

	@Resource
	private SessionInfoServiceImp ssi;

	// 登录校验
	@RequestMapping("/login")
	public ModelAndView login(User user, ModelAndView mv, HttpSession session) {
		List<User> list = this.loginService.findNameAndPass(null);
		for (User u : list) {
			if (u.getUserName().equals(user.getUserName())
					&& u.getLoginPass().equals(user.getLoginPass())) {
				session.setAttribute("userId", u.getUserId());
				session.setAttribute("level", u.getUserLevel());
				System.out.println(u.getUserLevel());
				session.setAttribute("info", user);
				this.sessionHandlerByCacheMap(session);
				mv.setViewName("home/fmain");
				return mv;

			}
		}
		mv.addObject("error", "登录名或密码有错！");
		mv.setViewName("login");
		return mv;

	}

	// 登出界面
	@RequestMapping("/logout")
	public String logout() {
		return "login";
	}

	// 主页面的主页
	@RequestMapping("/fmain")
	public String fmain() {
		return "home/fmain";
	}

	// 主页面的上层
	@RequestMapping("/top")
	public String top() {
		return "home/title";
	}

	// 主页面的左边
	@RequestMapping("/left")
	public String left() {
		return "home/left";
	}

	// 主页面的左边
	@RequestMapping("/olmsgList")
	public String olmsgList() {
		return "home/olmsgList";
	}
	//
	@RequestMapping("/olmsgUpdate")
	public String olmsgUpdate(){
		return "home/olmsgCreate";
	}
	//个人设置界面
	@RequestMapping("/proSet")
	public String proSet(){
		return "home/proset";
	}
	//
	@RequestMapping("/updatePass")
	public String updatePass(){
		return "home/updatePass";
	}
	
	
	

	// 加入唯一的sessionID
	public void sessionHandlerByCacheMap(HttpSession session) {
		String userid = session.getAttribute("userId").toString();
		if (RightListener.sessionContext.getSessionMap().get(userid) != null) {
			HttpSession userSession = (HttpSession) RightListener.sessionContext
					.getSessionMap().get(userid);
			// 注销在线用户
			if (!session.equals(userSession)) {
				userSession.invalidate();
				RightListener.sessionContext.getSessionMap().remove(
						session.getId());
				RightListener.sessionContext.getSessionMap().put(userid,
						session);
			}
			// 清除在线用户后，更新map,替换map sessionid

		} else {
			// 根据当前sessionid 取session对象。 更新map key=用户名 value=session对象 删除map
			RightListener.sessionContext.getSessionMap().get(session.getId());
			RightListener.sessionContext.getSessionMap().put(
					userid,
					RightListener.sessionContext.getSessionMap().get(
							session.getId()));
			RightListener.sessionContext.getSessionMap()
					.remove(session.getId());
		}
	}

}
